//
//  ECOrderTopShowEditViewController.m
//  ECERP.CRM
//
//  Created by 刘思麟 on 2018/11/19.
//  Copyright © 2018 Mac. All rights reserved.
//

#import "ECOrderTopShowEditViewController.h"
#import "ECOrderMainTopEditCollectionViewCell.h"

@interface ECPriHeaderView:UICollectionReusableView

@property (nonatomic,strong) UILabel * titleLB;

@property (nonatomic,strong) UILabel * detailLB;

@end

@implementation ECPriHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    WS(weakSelf, self);
//    self.backgroundColor = ECCOLOR(0xffffff);
    self.titleLB = [UILabel new];
    self.titleLB.textColor = ECCOLOR(0x333333);
    self.titleLB.font = ECFONT(13);
    [self addSubview:self.titleLB];
    [self.titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf);
        make.left.mas_equalTo(weakSelf).mas_offset(16 * PX_RELATIVE375);
    }];
    
    self.detailLB = [UILabel new];
    self.detailLB.textColor = ECCOLOR(0xB3B3B3);
    self.detailLB.font = ECFONT(13);
    [self addSubview:self.detailLB];
    [self.detailLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf);
        make.left.mas_equalTo(weakSelf.titleLB.mas_right).mas_offset(0 * PX_RELATIVE375);
    }];
}

@end

@interface ECPriFooterView:UICollectionReusableView

@property (nonatomic,strong) UIView * topView;

@end

@implementation ECPriFooterView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.topView = [UIView new];
    self.topView.backgroundColor = ECCOLOR(0xffffff);
    self.backgroundColor = ECCOLOR(0xF4F4F4);
    [self addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(15 * PX_RELATIVE375);
    }];
}

@end

@interface ECOrderTopShowEditViewController ()<UICollectionViewDelegate,UICollectionViewDataSource> {
    UIView * v;
}

@property (nonatomic,strong) UICollectionView * collectionView;

@property (nonatomic,strong) UICollectionViewFlowLayout * layOut;

@property (nonatomic,strong) NSMutableArray * topArr;

@property (nonatomic,strong) NSMutableArray * lastArr;

@end

@implementation ECOrderTopShowEditViewController

- (void)viewDidLoad {
    self.navigationItem.title = @"我的功能编辑";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:nil];
    [super viewDidLoad];
    self.view.backgroundColor = ECCOLOR(0xF4F4F4);
}

- (void)loadSubView {
    _topArr = @[@"1",@"2",@"3"].mutableCopy;
    _lastArr = @[@"4",@"5",@"6",@"7",@"8"].mutableCopy;
    
    WS(weakSelf, self);
    v = [[UIView alloc] init];
    v.backgroundColor = ECCOLOR(0xffffff);
    [self.view insertSubview:v atIndex:0];
    //    [self.collectionView addSubview:v];
    v.frame = CGRectMake(0, 0, 1, 1);
    
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(weakSelf.view);
        if (@available(iOS 11.0,*)) {
            make.bottom.mas_equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom);
        }else {
            make.bottom.mas_equalTo(weakSelf.view);
        }
    }];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
}

#pragma mark UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return self.topArr.count;
    }
    return self.lastArr.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        ECPriHeaderView * header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        if (indexPath.section == 0) {
            header.titleLB.text = @"我的功能";
            header.detailLB.text = @"（按住拖动调整排序）";
        }else {
            header.titleLB.text = @"备选功能";
            header.detailLB.text = @"（按住拖动至我的功能）";
        }
        return header;
    }
    ECPriFooterView * footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
    return footer;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ECOrderMainTopEditCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        cell.cellStyle = ECOrderMainTopEditCollectionViewCellStyleDelete;
    }else {
        cell.cellStyle = ECOrderMainTopEditCollectionViewCellStyleAdd;
    }
    WS(weakSelf, self);
    switch (cell.cellStyle) {
        case ECOrderMainTopEditCollectionViewCellStyleDelete:
            {
                cell.doActionBlock = ^(ECOrderMainTopEditCollectionViewCell * _Nonnull cell) {
                    NSInteger len = weakSelf.lastArr.count;
                    NSIndexPath * indexPath = [collectionView indexPathForCell:cell];
                    id t = weakSelf.topArr[indexPath.row];
                    [weakSelf.topArr removeObject:t];
                    [weakSelf.lastArr addObject:t];
                    [collectionView moveItemAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForRow:len inSection:1]];
                    [collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:len inSection:1]]];
                };
            }
            break;
        case ECOrderMainTopEditCollectionViewCellStyleAdd:{
            cell.doActionBlock = ^(ECOrderMainTopEditCollectionViewCell * _Nonnull cell) {
                NSInteger len = weakSelf.topArr.count;
                NSIndexPath * indexPath = [collectionView indexPathForCell:cell];
                if (len < 4) {
                    id t = weakSelf.lastArr[indexPath.row];
                    [weakSelf.lastArr removeObject:t];
                    [weakSelf.topArr addObject:t];
                    [collectionView moveItemAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForRow:len inSection:0]];
                    [collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:len inSection:0]]];
                }else {
                    return;
                }
            };
        }
            break;
        default:
            break;
    }
    
    return cell;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layOut];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[ECPriHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
        [_collectionView registerClass:[ECPriFooterView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
        [_collectionView registerClass:[ECOrderMainTopEditCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        [_collectionView addObserverBlockForKeyPath:@"contentSize" block:^(id  _Nonnull obj, id  _Nullable oldVal, id  _Nullable newVal) {
            NSString * str = [newVal description];
            NSString * sizeStr = [str componentsSeparatedByString:@":"].lastObject;
            CGSize size = CGSizeFromString(sizeStr);
            self -> v.size = size;
        }];
    }
    return _collectionView;
}

- (UICollectionViewFlowLayout *)layOut {
    if (!_layOut) {
        _layOut = [[UICollectionViewFlowLayout alloc] init];
        _layOut.headerReferenceSize = CGSizeMake(ECSCREEN_WIDTH, 30 * PX_RELATIVE375);
        _layOut.footerReferenceSize = CGSizeMake(ECSCREEN_WIDTH, 25 * PX_RELATIVE375);
        _layOut.minimumInteritemSpacing = 1 * PX_RELATIVE375;
        _layOut.minimumLineSpacing = 9 *PX_RELATIVE375;
        _layOut.sectionInset = UIEdgeInsetsMake(0, 5 * PX_RELATIVE375, 0, 5 * PX_RELATIVE375);
        _layOut.itemSize = CGSizeMake(85 * PX_RELATIVE375, 59 * PX_RELATIVE375);
    }
    return _layOut;
}

@end
